package com.ruoyi.livedemo.service;

import com.ruoyi.livedemo.dto.PartnerEvaluationRequest;
import com.ruoyi.livedemo.entity.PartnerEvaluation;
import com.ruoyi.livedemo.mapper.PartnerEvaluationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class EvaluationService {

    private final PartnerEvaluationMapper partnerEvaluationMapper;

    public PartnerEvaluation latest(Long userId) {
        return partnerEvaluationMapper.findLatestByUser(userId);
    }

    @Transactional
    public PartnerEvaluation save(Long userId, PartnerEvaluationRequest request) {
        PartnerEvaluation latest = partnerEvaluationMapper.findLatestByUser(userId);
        if (latest == null) {
            latest = new PartnerEvaluation();
            latest.setUserId(userId);
            latest.setPartnerName(request.getPartnerName());
            latest.setStrengths(request.getStrengths());
            latest.setImprovements(request.getImprovements());
            latest.setAppreciation(request.getAppreciation());
            partnerEvaluationMapper.insert(latest);
        } else {
            latest.setPartnerName(request.getPartnerName());
            latest.setStrengths(request.getStrengths());
            latest.setImprovements(request.getImprovements());
            latest.setAppreciation(request.getAppreciation());
            partnerEvaluationMapper.update(latest);
        }
        return latest;
    }
}


